package car.slyc.controller;

import car.slyc.model.Manager;
import car.slyc.service.ManagerServiceImpl;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class ManagerController {


    @Resource
    private ManagerServiceImpl service;

   //  登录
    @RequestMapping("/manager/login")
    public String login(@RequestParam("username") String username,
                        @RequestParam("pass") String pass,
                        Model model, HttpSession session,
                        HttpServletRequest  request) {

        System.out.println("是否执行登录");
        System.out.println(username);
        System.out.println(pass);

        Manager m = service.getByUsername(username);
        System.out.println(m != null);
        if (m != null) {
            System.out.println(m.getPass().equals(pass));
            if (m.getPass().equals(pass)) {
                session.setAttribute("manager", m);
                return "them14/index";
            } else {
                model.addAttribute("error_msg1", "密码错误");
                return "them14/login";
            }
        } else {
            model.addAttribute("error_msg1", "无此用户");
            return "them14/login";
        }
    }



    //  打开登录页面
    @RequestMapping("/")
    public String logins() {

        return "them14/login";

    }

    //进入欢迎页面或者首页
    @RequestMapping("/main")
    public ModelAndView main(HttpSession session, ModelAndView modelAndView) {
        modelAndView.setViewName("them14/main");
        return modelAndView;
    }


    //退出
    @RequestMapping("/exit")

    public ModelAndView exit(HttpSession session, ModelAndView modelAndView) {
        //session.setAttribute("manager", null);
        session.removeAttribute("manager");
        System.out.println("管理员信息被清空：" + session.getAttribute("manager"));
        modelAndView.setViewName("them14/login");
        return modelAndView;

    }



}
